////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.errors
{

import flash.utils.getClassName;

import ru.goodcore.utils.getCallerInfo;

/**
 * Base framework Error class
 *
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					genericerror, error, genericerrorcode
 */
public class GenericError extends Error {

	//[ResourceBundle('GenericError')]

	//-------------------------------------------------------------------------
	//
	//  Private class constants
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private static const RX_CLASS_NAME:		RegExp = /%className/g;

	/**
	 * @private
	 */
	private static const RX_METHOD_NAME:	RegExp = /%methodName|%className/g;

	/**
	 * @private
	 */
	private static const RX_PROPERTY_NAME:	RegExp = /%propertyName|%methodName/g;



	//-------------------------------------------------------------------------
	//
	//  Public class methods
	//
	//-------------------------------------------------------------------------

	/**
	 * @param errorID
	 * @param replaces	wild %1~%N will be replaced with the rest of parameters
	 * @return			formatted localized message
	 */
	public static function formatMessage(className:String, errorID:int, replaces:Array = null):String {

		/**
		 *  TODO Брать строку сообщения из ResourceManager
		 *  Определять bundleName по имени класса
		 */
		// var message:	String = ResourceManager.getInstance().getString(super.name, errorID.toString());
		//var manager:ResourceManager = getResourceManager();
		//var message:	String = errorID.toString();
		//var message:	String = manager.getString(className, errorID.toString(), replaces);

		if (errorID < 4000) { // Native FP error
			var message:	String = Error.getErrorMessage(errorID);

			switch (errorID) {
				case GenericErrorCode.CLASS_CANNOT_BE_INSTANTIATED:
					message = message.replace(/%1/g, '%className');
					break;
			}

		} else {
			message = Error.getErrorMessage(errorID);
		}

		var info:XML = getCallerInfo(3);

		if (info) {
			var className:String = info.@name.toXMLString();
			if (className)
				message = message.replace( RX_CLASS_NAME, className );

			var methodName:String = info.method.@name.toXMLString();
			if (methodName)
				message = message.replace( RX_METHOD_NAME, methodName );

			var propertyName:String = info.accessor.@name.toXMLString();
			if (propertyName)
				message = message.replace( RX_PROPERTY_NAME, propertyName );
		}

		for (var i:uint = 0; i < replaces.length; i++) {
			message = message.replace(new RegExp('%' + i, 'g'), replaces[i]);
		}

		return message;
	}

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 *  Constructor
	 */
	public function GenericError(errorID:int, ...replaces) {
		super( '', errorID );

		super.name = getClassName(this);
		super.message = formatMessage(super.name, errorID, replaces);
	}

	//-------------------------------------------------------------------------
	//
	//  Public methods
	//
	//-------------------------------------------------------------------------

	/**
	 * @return formatted error string
	 */
	public function toString():String {
		return super.name + " #" + super.errorID + ": " + super.message;
	}

}
}
